package ttsolver.variables;

import ttsolver.input.Node;
import ttsolver.input.Train;

public class SDcalc extends DefaultVariable {

	private Train train;
	private Node node;
	private double schedTime;

	public SDcalc() {
		super(VariableType.SCHED_DEV);
	}

	public SDcalc(Train train, Node node, double schedTime) {
		super(VariableType.SCHED_DEV);
		this.train = train;
		this.node = node;
		this.schedTime = schedTime;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}

	public double getSchedTime() {
		return schedTime;
	}

	public void setSchedTime(double schedTime) {
		this.schedTime = schedTime;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((train == null) ? 0 : train.getId().intValue());
		result = prime * result
				+ ((node == null) ? 0 : node.getId().intValue());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		SDcalc other = (SDcalc) obj;

		if (train == null) {
			if (other.train != null)
				return false;
		} else if (!train.equals(other.train)) {
			return false;
		}

		if (node == null) {
			if (other.node != null)
				return false;
		} else if (!node.equals(other.node)) {
			return false;
		}
		return true;
	}

	@Override
	public int compareTo(Object obj) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		SDcalc other = (SDcalc) obj;

		if (this == other)
			return EQUAL;

		if (this.train != null && other.train != null) {
			if (this.train.getId() < other.train.getId())
				return BEFORE;
			if (this.train.getId() > other.train.getId())
				return AFTER;
		}

		if (this.node != null && other.node != null) {
			if (this.node.getId() < other.node.getId())
				return BEFORE;
			if (this.node.getId() > other.node.getId())
				return AFTER;
		}

		return EQUAL;
	}

	@Override
	public String toString() {
		if (!printVar)
			return "";

		String s = (train != null) ? String.valueOf(train.getId()) : "";
		s += (node != null) ? "," + String.valueOf(node.getId()) : "";
		return "SD_(" + s + ")";
	}
}
